package com.itcast.controller;

import com.itcast.pojo.Result;
import com.itcast.pojo.User;
import com.itcast.service.UserService;
import com.itcast.utils.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
public class LoginController {
    @Autowired
    private UserService userService;

    /**
     * 登录操作，返回令牌
     * @param username
     * @param password
     * @return
     */
    @GetMapping("/login")
    public Result login(String username,String password){
        User user = userService.login(username,password);
        if (user != null){
            Map<String,Object> mes = new HashMap<>();
            mes.put("id",user.getId());
            String jwt = JwtUtils.generateJwt(mes);
            return Result.success(jwt);
        }
        return Result.error("NOT_LOGIN");
    }
}
